package com.gomtel.smartdevice.ui.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.base.WatchAppBaseActivity;
import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.global.DataClass;
import com.gomtel.smartdevice.moudle.db.entity.LoginUserInfo;
import com.gomtel.smartdevice.moudle.db.entity.SignsOfDataInfo;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.moudle.rxtools.EventBusUtil;
import com.gomtel.smartdevice.ui.dialog.ShowDialog;
import com.gomtel.smartdevice.ui.view.CircleImageView;
import com.gomtel.smartdevice.utils.AlbumUtils;
import com.gomtel.smartdevice.utils.LogUtil;
import com.gomtel.smartdevice.utils.SystemUtil;
import com.gomtel.smartdevice.widget.AlbumCacheClass;
import com.gomtel.smartdevice.widget.CustomPopupWindow;
import com.gomtel.smartdevice.widget.DockingSymbol;
import com.yanzhenjie.album.AlbumFile;
import com.yanzhenjie.durban.Durban;

import java.util.ArrayList;

import butterknife.BindView;

import static com.gomtel.smartdevice.global.DataClass.mStringAndBitmap;

/**
 * Created by Administrator on 2018/5/15 0015.
 * 个人中心
 */

public class PersonageActivtiy extends WatchAppBaseActivity implements View.OnClickListener, CustomPopupWindow.OnItemClickListener, PopupWindow.OnDismissListener {

    @BindView(R.id.img_btn_black)
    ImageButton img_btn_black;
    @BindView(R.id.photo_view)
    CircleImageView photo_view;
    @BindView(R.id.nickname)
    EditText nickname;

    @BindView(R.id.gender)
    TextView gender;
    @BindView(R.id.birthday)
    TextView birthday;

    @BindView(R.id.prisonage_height)
    EditText prisonage_height;
    @BindView(R.id.prisonage_weight)
    EditText prisonage_weight;
    @BindView(R.id.brachium)
    EditText brachium;
    @BindView(R.id.commit)
    TextView commit;
    private ShowDialog showDialog;
    private CustomPopupWindow customPopupWindow;
    private AlbumCacheClass albumCacheClass;
    private AlbumUtils albumUtils;
    private DataClass dataClass;
    private String openActivity;
    private SignsOfDataInfo signsOfDataInfo;
    private int stepCountStandard = 5000;  //计步标准
    private int caloryStandard = 300000;     //卡路里标准
    private int pvhMmHgStandard = 130;    //收缩压标准
    private int ldoMmHgStandard = 70;    //舒张压标准
    private LoginUserInfo loginUserInfo;

    @Override
    protected void registerEvent(CommonEvent commonevent) {
        switch (commonevent.getCode()) {
            case EventCode.GENDER:
                switch (commonevent.getTemp_value()) {
                    case 0:
                        gender.setText(getString(R.string.man));
                        break;
                    case 1:
                        gender.setText(getString(R.string.woman));
                        break;
                }
                break;
            case EventCode.SELECTTIME:
                birthday.setText(commonevent.getTemp_str());
                break;
            case EventCode.OPEN_PICTURE:
                albumUtils.singlePictureSelect(this, albumCacheClass);
                break;

        }

    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_personage;
    }

    @Override
    protected void initClass() {
        showDialog = ShowDialog.newInstance();
        customPopupWindow = new CustomPopupWindow(this);
        albumCacheClass = new AlbumCacheClass();
        albumUtils = AlbumUtils.newInstance();
        openActivity = getIntent().getStringExtra("openActivity");
    }

    @Override
    protected void initData() {
        dataClass = new DataClass(dataManager);
        signsOfDataInfo = dataManager.querySignsOfDataInfo(UserContent.UserName);
        loginUserInfo = dataManager.queryLoginUserInfo(UserContent.UserName);
    }

    @Override
    protected void initView() {
        if (openActivity != null) {
            nickname.setText(loginUserInfo.getNickName());
            birthday.setText("1991-01-01");
            prisonage_height.setText("180");
            prisonage_weight.setText("120");
            gender.setText(loginUserInfo.getSex());
            brachium.setText("180");
            Glide.with(this).load(loginUserInfo.getUserPhoto()).asBitmap().into(photo_view);
        } else {
            SignsOfDataInfo signsOfDataInfo = dataManager.querySignsOfDataInfo(UserContent.UserName);
            nickname.setText(signsOfDataInfo.getNickName());
            gender.setText(signsOfDataInfo.getSex());
            birthday.setText(signsOfDataInfo.getBirthday());
            prisonage_height.setText(String.valueOf(signsOfDataInfo.getHeight()));
            prisonage_weight.setText(String.valueOf(signsOfDataInfo.getWeight()));
            brachium.setText(String.valueOf(signsOfDataInfo.getBrachium()));
            photo_view.setImageBitmap(mStringAndBitmap.stringToBitmap(signsOfDataInfo.getUserPhoto()));
        }
    }

    @Override
    protected void initListener() {
        gender.setOnClickListener(this);
        birthday.setOnClickListener(this);
        nickname.setOnClickListener(this);
        img_btn_black.setOnClickListener(this);
        photo_view.setOnClickListener(this);
        customPopupWindow.setOnItemClickListener(this);
        customPopupWindow.setOnDismissListener(this);
        commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.gender:
                showDialog.showGenderDialog(this, 0);
                break;
            case R.id.birthday:
                showDialog.showDatepickerDialog(this, "1991-01-01");
                break;
            case R.id.img_btn_black:
                if (openActivity == null)
                    finish();
                break;
            case R.id.photo_view:
                customPopupWindow.showAtLocation(findViewById(R.id.brachium), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 100);
                SystemUtil.windowToDark(this);
                break;
            case R.id.commit:
                if (IsEmptyJudge()) {
                    if (signsOfDataInfo != null) {
                        stepCountStandard = signsOfDataInfo.getStepCountStandard();
                        caloryStandard = signsOfDataInfo.getCaloryStandard();
                        pvhMmHgStandard = signsOfDataInfo.getPvhMmHgStandard();
                        ldoMmHgStandard = signsOfDataInfo.getLdoMmHgStandard();
                    }
                    dataClass.InitSignsOfDataInfo(loginUserInfo.getUsername(), nickname.getText().toString(), gender.getText().toString(), birthday.getText().toString(),
                            Integer.valueOf(prisonage_height.getText().toString().replace(" ", "")), Integer.valueOf(prisonage_weight.getText().toString().replace(" ", "")),
                            Integer.valueOf(brachium.getText().toString().replace(" ", "")), stepCountStandard, caloryStandard, pvhMmHgStandard, ldoMmHgStandard, ((BitmapDrawable) photo_view.getDrawable()).getBitmap());
                    if (openActivity != null) {
                        startActivity(new Intent(PersonageActivtiy.this, HomeActivity.class));
                    }
                    EventBusUtil.sendEvent(new CommonEvent(EventCode.SETTING_SIGNS_OF_DATAINFO));
                    EventBusUtil.sendEvent(new CommonEvent(EventCode.PERSONAGE_UP_DATA));
                    finish();
                } else {
                    toastUtil.showToast("请填写完整信息");
                }
                break;
        }
    }

    @Override
    public void setOnItemClick(View v) {
        switch (v.getId()) {
            case R.id.id_btn_select:
                albumUtils.singlePictureSelect(this, albumCacheClass);
                break;
            case R.id.id_btn_take_photo:
                albumUtils.TakePhoto(this);
                break;
            case R.id.id_btn_cancelo:
                break;
        }
        customPopupWindow.dismiss();
    }

    @Override
    public void onDismiss() {
        SystemUtil.windowToLight(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtil.e(TAG, "resultCode : " + resultCode);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case DockingSymbol.TAKEPHOTO:
                    String picturePath = data.getStringExtra("picturePath");
                    ArrayList<AlbumFile> albumFileselectList = albumCacheClass.getAlbumFileselectList();
                    ArrayList<String> pictureFiles = albumCacheClass.getPictureFiles();
                    pictureFiles.clear();
                    AlbumFile albumFile = new AlbumFile();
                    albumFile.setPath(picturePath);
                    albumFileselectList.add(albumFile);
                    pictureFiles.add(picturePath);
                    albumUtils.tailoring(pictureFiles, this);
                    break;
                case DockingSymbol.ALBUM_CODE_TAILORING:
                    String photoFile = Durban.parseResult(data).get(0);
                    Glide.with(this).load(photoFile).asBitmap().into(photo_view);
                    break;
            }
        }
    }

    public boolean IsEmptyJudge() {
        boolean status = false;
        if (getViewString(nickname) || getViewString(gender) || getViewString(birthday) || getViewString(prisonage_height) || getViewString(prisonage_weight) || getViewString(brachium))
            status = true;
        return status;
    }

    public boolean getViewString(TextView textView) {
        boolean status = true;
        if (textView.getText().toString().isEmpty())
            status = false;
        return status;
    }
}
